﻿package fui{
	import flash.events.*;
	import flash.display.Sprite;
	import fl.controls.*;
	import fl.containers.ScrollPane;
	import flash.text.TextFormat;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import fui.*;
	import fui.FUIData.*;
	import fui.FUIControllers.*;
	import fui.FUIComponents.*;
	import fui.FUIControllers.FUIMenuEvent;
	import fui.Configuration;
//	import mx.controls.TileList;
//	import fui.utils.calendar.*;
//	import fui.utils.FUICalendar;
//

//import biz.Flashscript.components.datechooser.DateChooserModul;
	public class main extends Sprite{
		private var _path:String = "http://b-o-g-o/";
		private var _generals_xml:XML;
		private var _leftTab:int = 50;
		private var _topTab:int = 50;
		private var _title:Label;
		private var _loginPane:ScrollPane;
		private var _menuPane:ScrollPane;
		private var _msgrPane:ScrollPane;
		private var _mainPane:ScrollPane;
		private var _exchangePane:ScrollPane;
		
		private var _cfg:Configuration;
		
		private var _verLbl:Label;
		private var _uis:UISession;
		private var _login:LoginView;
		private var _componentsController:FUIComponentsController;
		private var _menuView:FUIMenu;
		private var _serverName:String = "b-o-g-o.com";
		private var _RootSprite:Sprite = Sprite(this);
		private var dbc:IDBClient = DBClient.getInstance(_serverName, _RootSprite);
		private var dbc2:IDBClient = DBClientV2.getInstance(_serverName, _RootSprite);
		public function main(){
			_cfg = new Configuration(_path + "xml/generals.xml");
			_cfg.addEventListener("CONFLOADED", loadAll);//loadAll);
			//dbc2.addEventListener(Event.COMPLETE, dbresponse);
			//dbc2.CallStoredProcedure("add_new_player", "Kaka", "kaka", "shmiaka", "12345", "Male", "30", 0, "Isr", "TA", "haddas", "2345", "velund@gmail.com", "04324234", "0234234234");
			//dbc.addEventListener(Event.COMPLETE, dbresponse);
			//dbc.CallStoredProcedure("verify_user_by_name", "Dalt", "12345");
			
		}
		
		public function dbresponse(event:Event):void
		{
			trace("DB request complited");
		}
		
		private function loadAll(event:Event):void{
			_path 		= _cfg.host+"/"+_cfg.webdir;
			_serverName = _cfg.host+"/"+_cfg.webdir;
			
			createView();
			
			initObjects();
			
			
			_login.addEventListener("LOGGEDIN", logindone);
			trace(_cfg.host);
		}
		function completeHandler(event:Event):void {
			trace("completeHandler");
			try {
				trace(event.target.data);
				this._generals_xml = new XML(event.target.data);
				//path_ = _section_xml.path;
 				var XMLnames:XMLList = _generals_xml.name
				trace(XMLnames);
				var xparams:Array = new Array();
				
				trace("---------------------");
				var i:int;
				var namesLength:int = XMLnames.length();
				trace("Num of names: " + namesLength);
					for (i = 0; i < namesLength; i++) 
					{
						trace({src:XMLnames[i].text()});
						xparams.push({src:XMLnames[i].text()});
					}
//				trace(xparams[0].src);
				//trace(xparams[1]);
			} catch (error:Error) {
				trace(error.message);
			}
		}
		private function initObjects():void{
			trace("initObjects");
			_uis = new UISession();
			_uis.dropSession();
			_login = new LoginView(_uis, 1,1, this.dbc2);
			_loginPane.addChild(_login);
			_componentsController = new FUIComponentsController(_uis);
			trace("FUIComponentsController");
			var registration:Profile = new Profile(1, this.dbc2);
			var uprofile:Profile = new Profile(2, this.dbc2);
			var rnaks:RankList = new RankList();
			trace("rnaks:" + rnaks);
			var crGm:CreateGame =  new CreateGame(this.dbc2);
			var glist:GamesList = new GamesList();
			_componentsController.registerNewComponent(registration);
			_mainPane.addChild(registration);
			_componentsController.registerNewComponent(uprofile);
			_mainPane.addChild(uprofile);
			
			_componentsController.registerNewComponent(rnaks);
			_mainPane.addChild(rnaks);

			_componentsController.registerNewComponent(crGm);
			_mainPane.addChild(crGm);
			
			_componentsController.registerNewComponent(glist);
			_mainPane.addChild(glist);
			
			_menuView = new FUIMenu(_componentsController.cNames);

			_menuView.addEventListener("onMenuClick", onMenuChoice);
			_menuPane.addChild(_menuView);
		}
		
		private function createView():void{
			_verLbl = new Label();
			_verLbl.text = "v_0.1.1";
			_verLbl.x = _leftTab;
			_verLbl.y = 10 + _topTab;
			addChild(_verLbl);
			_title = new Label();
			var format:TextFormat = new TextFormat();
          	//format.font		      = "foo";
          	format.color          = 0xAAFFFF;
          	format.size           = 20;
			_title.x = 250 +_leftTab;
			_title.y = 10 + _topTab;
			_title.setSize(700, 30);
			_title.text = _cfg.title;//"  Welcome to Money Pool Comunity Site ! ";
			_title.setStyle("textFormat", format);
			addChild(_title);
			_loginPane = new ScrollPane();
			_loginPane.scaleX = 1;
			_loginPane.scaleY = 1;
			_loginPane.x = 5 +_leftTab;
			_loginPane.y = 40 + _topTab;
			_loginPane.setSize(150, 119);
			addChild(_loginPane);
			_menuPane = new ScrollPane();
			_menuPane.scaleX = 1;
			_menuPane.scaleY = 1;
			_menuPane.x = 5 +_leftTab;
			_menuPane.y = 165 + _topTab;
			_menuPane.setSize(150, 340);
			addChild(_menuPane);
			_msgrPane = new ScrollPane();
			_msgrPane.scaleX = 1;
			_msgrPane.scaleY = 1;
			_msgrPane.x = 5 +_leftTab;
			_msgrPane.y = 167 + 341 + _topTab;
			_msgrPane.setSize(150, 132);
			addChild(_msgrPane);
			
			_mainPane = new ScrollPane();
			_mainPane.scaleX = 1;
			_mainPane.scaleY = 1;
			_mainPane.x = 160 +_leftTab;
			_mainPane.y = 40 + _topTab;
			_mainPane.setSize(550, 600);
			//_mainPane.opaqueBackground = 1;

			//_mainPane.opaqueBackground = 0xE0F4FC;
			addChild(_mainPane);
			_exchangePane = new ScrollPane();
			_exchangePane.scaleX = 1;
			_exchangePane.scaleY = 1;
			_exchangePane.x = 715 +_leftTab;
			_exchangePane.y = 40 + _topTab;
			_exchangePane.setSize(150, 600);
			addChild(_exchangePane);
			

		}
		public function logindone(event:Event):void
		{
			trace("Login done");
			_componentsController.rebuildCNames();
			_menuView.rebuildBtns();
			_login.removeEventListener("LOGGEDIN", logindone);
			_login.addEventListener("LOGGEDOUT", logoutdone);
			
		}
		public function logoutdone(event:Event):void
		{
			trace("###### Loged out ###");
			_login.removeEventListener("LOGGEDOUT", logindone);
			_login.addEventListener("LOGGEDIN", logindone);
			_componentsController.rebuildCNames();
			_menuView.rebuildBtns();
		}
		public function onMenuChoice(event:FUIMenuEvent):void
		{
			trace("From Menu:" + event.getContent());
			_componentsController.upComponent(event.getContent());
		}
	}
	
	
}